package Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

public class ABCServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //使用getParameter来获取前端的值
        System.out.println("使用getParameter来获取前端的值");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String hobbys[] = req.getParameterValues("hobby");
        System.out.println("您好，"+name);
        System.out.println("您的兴趣爱好有：");
        for (String hobby:hobbys){
            System.out.println(hobby);
        }
        System.out.println("=============================================================================");
        System.out.println("使用getParameterNames（）方法获取所有参数名,再通过参数名，获取该参数名的所有参数值");
        java.util.Enumeration<String> paramNames= req.getParameterNames();
        while (paramNames.hasMoreElements()){
            String paramName = paramNames.nextElement();
            String []parameterValues = req.getParameterValues(paramName);
                System.out.println("参数" + paramName + "有以下值");
                for (String paramValue : parameterValues) {
                    System.out.println(paramValue);
                }
        }
        System.out.println("=============================================================================");
        System.out.println("使用getParameterMap（）方法获取所有参数名,再通过参数名，获取该参数名的所有参数值");
        java.util.Map<String,String[]> paramMap=req.getParameterMap();
        for (Map.Entry<String,String[]> mapEntry:paramMap.entrySet()){
            String paramName=mapEntry.getKey();
            System.out.println("参数" + paramName + "有以下值");
            String[] paramvalues = mapEntry.getValue();
            for (String paramValue : paramvalues) {
                System.out.println(paramValue);
            }
        }
        System.out.println("=============================================================================");
        System.out.println("获取"+req.getMethod()+"请求行信息");
        System.out.println(req.getScheme());
        System.out.println(req.getMethod());
        System.out.println(req.getProtocol());
        System.out.println(req.getQueryString());
        String zhangSanURLEncodedString=java.net.URLEncoder.encode("张三","utf-8");
        String zhangsan=java.net.URLDecoder.decode("%E5%BC%A0%E4%B8%89","utf-8");
        System.out.println(zhangsan+"<->"+zhangSanURLEncodedString);



        System.out.println("=============================================================================");
        System.out.println("获取"+req.getMethod()+"请求行信息");
        String userAgentValue = req.getHeader("user-agent");
        System.out.println("user-agent请求头值是："+userAgentValue);
        java.util.Enumeration<String> headeraNames=req.getHeaderNames();
        while (headeraNames.hasMoreElements()){
            String headerName=headeraNames.nextElement();
            java.util.Enumeration<String> headerValues=req.getHeaders(headerName);
            System.out.println("请求头:"+headerName+"对应请求头值有：");
            while (headerValues.hasMoreElements()){
                String headerValuer=headerValues.nextElement();
                System.out.println(headerValuer);
            }
        }
        System.out.println("=============================================================================");
        System.out.println("获取web服务器主机以及浏览主机信息");
        System.out.println(req.getServerName());
        System.out.println(req.getServerPort());
        System.out.println(req.getLocalAddr());
        System.out.println(req.getLocalPort());
        System.out.println(req.getLocalName());
        System.out.println(req.getRemoteAddr());
        System.out.println(req.getRemotePort());
        System.out.println(req.getRemoteHost());
        java.util.Locale browserLocale=req.getLocale();
        System.out.println(browserLocale+"="+browserLocale.getCountry()+"="+browserLocale.getISO3Country());
        java.util.Enumeration<java.util.Locale> browserLocales=req.getLocales();
        while (browserLocales.hasMoreElements()){
         java.util.Locale locale=browserLocales.nextElement();
            System.out.println(locale+"#"+locale.getCountry()+"#"+locale.getCountry());
        }
        System.out.println("=============================================================================");
        System.out.println("获取URL Mappings信息");
        javax.servlet.http.HttpServletMapping mapping=req.getHttpServletMapping();
        String mapName=mapping.getMappingMatch().name();
        String value=mapping.getMatchValue();
        String pattern=mapping.getPattern();
        String servletName = mapping.getServletName();
        System.out.println("servlet名是："+servletName);
        System.out.println("URL映射类型是："+mapName);
        System.out.println("URL匹配的值是："+value);
        System.out.println("URL匹配规则是："+pattern);
        System.out.println("=============================================================================");
        System.out.println("请求转发的基本使用");
        java.util.Map<String,Object> map=new java.util.HashMap<String,Object>();
        map.put("status","1");
        map.put("message","登录成功");
        java.util.ArrayList<String>list=new java.util.ArrayList<String>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        map.put("list",list);
        req.setAttribute("map",map);
        System.out.println("请求转发的类型是："+req.getDispatcherType());
        req.getRequestDispatcher("BCDServlet").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
